import { defHttp } from '/@/utils/http/axios';
import { LoginReq, LoginResp, UserInfoResp } from './model/userModel';

import { ErrorMessageMode } from '/#/axios';
import { MICRO_SERVICE_ADMIN } from '/@/api/ServiceConstant';

enum Api {
  Login = '/admin/login',
  DisplayCaptchaImage = '/admin/captcha/showCaptchaImage',
  Logout = '/admin/logout',
  GetUserInfo = '/webservice/user/queryCurrentUserInfo',
  GetPermCode = '/getPermCode',
  TestRetry = '/testRetry',
}

/**
 * @description: user login api
 */
export function loginApi(params: LoginReq, mode: ErrorMessageMode = 'modal') {
  return defHttp.post<LoginResp>(
    {
      url: MICRO_SERVICE_ADMIN + Api.Login,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: 显示隐藏图像验证码
 */
export function displayCaptchaImage() {
  return defHttp.get<boolean>(
    { url: MICRO_SERVICE_ADMIN + Api.DisplayCaptchaImage },
    { errorMessageMode: 'none' },
  );
}

/**
 * @description: getUserInfo
 */
export function getUserInfo() {
  return defHttp.get<UserInfoResp>(
    { url: MICRO_SERVICE_ADMIN + Api.GetUserInfo },
    { errorMessageMode: 'none' },
  );
}

export function getPermCode() {
  return defHttp.get<string[]>({ url: Api.GetPermCode });
}

export function doLogout() {
  return defHttp.post({ url: MICRO_SERVICE_ADMIN + Api.Logout });
}

export function testRetry() {
  return defHttp.get(
    { url: Api.TestRetry },
    {
      retryRequest: {
        isOpenRetry: true,
        count: 5,
        waitTime: 1000,
      },
    },
  );
}
